﻿using BCCommon;
using BCCommon.Enums;
using BCData.HelpDocument.HelpDocumentCategory;
using BCDto.HelpDocument.HelpDocumentCategory;
using BCEntity.HelpDocument.HelpDocumentCategory;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;

namespace BCService.HelpDocument.HelpDocumentCategory
{
    public class HelpDocumentCategoryService : IHelpDocumentCategoryService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IHelpDocumentCategoryData helpDocumentCategoryData;
        private readonly string key = string.Format("{0}", EntityNameConst.HelpDocumentCategoryEntity);

        public HelpDocumentCategoryService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IHelpDocumentCategoryData helpDocumentCategoryData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.helpDocumentCategoryData = helpDocumentCategoryData;
        }

        #region 检查帮助文档类型是否存在
        public bool Exists(long helpDocumentCategoryId)
        {
            return this.helpDocumentCategoryData.Exists(helpDocumentCategoryId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加帮助文档类型
        public HelpDocumentCategoryDto Add(HelpDocumentCategoryRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<HelpDocumentCategoryEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            var result = this.helpDocumentCategoryData.Add(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<HelpDocumentCategoryDto>();
        }
        #endregion

        #region 修改帮助文档类型
        public HelpDocumentCategoryDto Update(HelpDocumentCategoryPutDto putDto,AdminDto adminDto)
        {
            if (!this.Exists(putDto.HelpDocumentCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<HelpDocumentCategoryEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.helpDocumentCategoryData.Update(entity).GetAwaiter().GetResult().As<HelpDocumentCategoryDto>();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 删除帮助文档类型
        public bool Delete(long helpDocumentCategoryId)
        {
            if (!this.Exists(helpDocumentCategoryId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.helpDocumentCategoryData.Delete(helpDocumentCategoryId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取帮助文档类型
        public HelpDocumentCategoryDto Get(long helpDocumentCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, helpDocumentCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.helpDocumentCategoryData.GetEntity(helpDocumentCategoryId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<HelpDocumentCategoryDto>();
        }
        #endregion

        #region 获取帮助文档类型列表
        public IEnumerable<HelpDocumentCategoryDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.helpDocumentCategoryData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<HelpDocumentCategoryDto>>();
        }
        #endregion

        #region 获取帮助文档类型分页
        public PageableList<HelpDocumentCategoryDto> Query(string documentCategoryName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, documentCategoryName, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.helpDocumentCategoryData.Query(documentCategoryName, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<HelpDocumentCategoryDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<HelpDocumentCategoryDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}